﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HongLingJin
{
    public class VisitorDAL
    {
        public int Insert(Visitor v)
        {
            List<string> listParaName = new List<string>();
            List<SQLiteParameter> listPara = new List<SQLiteParameter>();
            IniList(v, listParaName, listPara);
            string sql = "insert into v_visitor({0}) values({1});";
            sql = string.Format(sql, string.Join(",", listParaName), "@" + string.Join(",@", listParaName));
            return SQLiteHelper.ExecuteNonQuery(sql, listPara.ToArray());
        }

        private void IniList(Visitor v, List<string> listParaName, List<SQLiteParameter> listPara)
        {
            if (v.Id != null)
            {
                listParaName.Add("id");
                listPara.Add(new SQLiteParameter("@Id", v.Id));
            }

            if (v.VistorInfoId != null)
            {
                listParaName.Add("vistorInfoId");
                listPara.Add(new SQLiteParameter("@VistorInfoId", v.VistorInfoId));
            }

            if (v.Image_file != null)
            {
                listParaName.Add("image_file");
                listPara.Add(new SQLiteParameter("@Image_file", v.Image_file));
            }

            if (v.Tag_id_list != null)
            {
                listParaName.Add("tag_id_list");
                listPara.Add(new SQLiteParameter("@Tag_id_list", v.Tag_id_list));
            }

        }
    }
}
